﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.MSAS.ManagedAPI;
using Microsoft.Reputation.Tests.FrontEndCommon;

namespace Microsoft.Reputation.Tests.FrontEndCommon
{
    public static class CategorizeHelper
    {
        /// <summary>
        /// Converts a MrsDeliveryServiceProvider response into a form usable 
        /// by the ClientCategorizeProcessor
        /// </summary>
        /// <param name="response"></param>
        /// <returns></returns>
        public static SimpleReputationData[] ConvertToSimpleReputationData(MrsLookupResponse response)
        {
            List<SimpleReputationData> simpleReputationList = new List<SimpleReputationData>();

            foreach (MrsReputationObject reputationObject in response.Matches)
            {
                SimpleReputationData simpleRepData = new SimpleReputationData();

                MrsMetadata bloomMetadata = reputationObject.Metadata.Where(
                    meta => meta.Name == "Bloom").SingleOrDefault();
                if (bloomMetadata != null)
                {
                    simpleRepData.bloom = (byte[])bloomMetadata.Value;
                }

                simpleRepData.sha256 = (byte[])reputationObject.Metadata.Where(
                    meta => meta.Name == "SHA256").Single().Value;
                simpleRepData.categories = ConvertToInputCategory(reputationObject.Reputation);
                simpleReputationList.Add(simpleRepData);
            }

            return simpleReputationList.ToArray();
        }

        /// <summary>
        /// Converts a set of MrsDeliveryServiceProvider object reputations to InputCategories usable by
        /// the ClientCategorizeProcessor
        /// </summary>
        /// <param name="reputationList"></param>
        /// <returns></returns>
        private static InputCategory[] ConvertToInputCategory(IList<MrsCategoryReputation> reputationList)
        {
            List<InputCategory> result = new List<InputCategory>();

            if (reputationList == null)
            {
                return result.ToArray();    
            }

            foreach (MrsCategoryReputation categoryRep in reputationList)
            {
                string hostInh = (string)categoryRep.CategoryInfo.Where(
                    meta => meta.Name == "HostInheritance").SingleOrDefault().Value;
                string pathInh = (string)categoryRep.CategoryInfo.Where(
                    meta => meta.Name == "PathInheritance").SingleOrDefault().Value;

                InputCategory inputCat = new InputCategory
                {
                    categoryId = categoryRep.CategoryId,
                    confidence = categoryRep.Confidence,
                    hostInheritance = hostInh == "1" ? true : false,
                    pathInheritance = pathInh == "1" ? true : false
                };
                result.Add(inputCat);
            }

            return result.ToArray();
        }
    }
}
